package com.zf.shuashua.ui.popup;

import android.app.Activity;
import android.view.View;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.XPopupCallback;
import com.zf.shuashua.advert.AdvertConstants;
import com.zf.shuashua.advert.InteractionAdListener;
import com.zf.shuashua.advert.NativeAdvertManager;
import com.zf.shuashua.advert.OnRewardListener;
import com.zf.shuashua.advert.RewardAdvertManager;
import com.zf.shuashua.bean.Giftjson;

import java.lang.ref.WeakReference;

/**
 * 新手任务-弹框
 */
public class TaskPopupManager {

    private String mType;
    private WeakReference<Activity> weakReference;
    private TaskPopupListener mListener;

    public interface TaskPopupListener {
        void onAdVideoBarClick();

        void onAdClose();

        void onVideoError();
    }

    private static TaskPopupManager instance;

    private TaskPopupManager() {

    }

    public static TaskPopupManager getInstance() {
        if (null == instance) {
            synchronized (TaskPopupManager.class) {
                if (null == instance) {
                    instance = new TaskPopupManager();
                }
            }
        }
        return instance;
    }

    public synchronized void showPopup(Activity activity, String type, TaskPopupListener listener) {
        this.mType = type;
        this.mListener = listener;
        this.weakReference = new WeakReference<>(activity);
        Giftjson result = getData();
        GuidePopup taskPopup = new GuidePopup(weakReference.get(), result.getTitle(), result.getSubTitle(), result.getValue(), result.getUiType() + "");
        new XPopup.Builder(weakReference.get())
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .setPopupCallback(new XPopupCallback() {
                    @Override
                    public void onCreated() {

                    }

                    @Override
                    public void beforeShow() {

                    }

                    @Override
                    public void onShow() {
                        NativeAdvertManager.getInstance().loadExpressAd(weakReference.get(), new InteractionAdListener() {
                            @Override
                            public void onRenderSuccess(View view) {
                                taskPopup.addAdvertView(view);
                            }

                            @Override
                            public void onError() {
                                taskPopup.loadAdvertFail();
                            }
                        });
                    }

                    @Override
                    public void onDismiss() {

                    }

                    @Override
                    public boolean onBackPressed() {
                        return false;
                    }
                })
                .asCustom(taskPopup)
                .show();

        taskPopup.setPopupListener(new OnPopupListener() {
            @Override
            public void onNext() {
                taskPopup.dismiss();
                showAdvert();
            }

            @Override
            public void onClose() {

            }
        });
    }

    private void showAdvert() {
        RewardAdvertManager.getInstance().showAdvert(weakReference.get(), AdvertConstants.REWARD_TYPE_TASK, new OnRewardListener() {
                    @Override
                    public void onAdClose() {
                        weakReference.get().runOnUiThread(() -> {
                            if (mListener != null) {
                                mListener.onAdClose();
                            }
                        });
                    }

                    @Override
                    public void onAdClick() {
                        if (mListener != null) {
                            mListener.onAdVideoBarClick();
                        }
                    }

                    @Override
                    public void onVideoComplete() {

                    }

                    @Override
                    public void onVideoError() {
                        weakReference.get().runOnUiThread(() -> {
                            if (mListener != null) {
                                mListener.onVideoError();
                            }
                        });
                    }
                });
    }

    private Giftjson getData() {
        Giftjson giftjson = new Giftjson();
        switch (mType) {
            case TaskPopupConstants.TYPE_EXCLUSIVE:
                giftjson.setTitle("专属视频");
                giftjson.setSubTitle("领三倍奖励");
                giftjson.setValue("288");
                giftjson.setUiType(0);
                giftjson.setType("PUNCH");
                giftjson.setPopType("PUNCH");
                break;
            case TaskPopupConstants.TYPE_PUNCH:
                giftjson.setTitle("签到成功");
                giftjson.setSubTitle("领取翻倍奖励");
                giftjson.setValue("100");
                giftjson.setUiType(0);
                giftjson.setType("PUNCH");
                giftjson.setPopType("PUNCH");
                break;
            case TaskPopupConstants.TYPE_PUNCH_DOUBLE:
                giftjson.setTitle("翻倍奖励");
                giftjson.setSubTitle("去赚更多");
                giftjson.setValue("150");
                giftjson.setUiType(0);
                giftjson.setType("PUNCH");
                giftjson.setPopType("DoubleReward_PUNCH");
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + mType);
        }
        return giftjson;
    }

}
